<?php

namespace wicked\core;

use wicked\core\Event;
use wicked\tools\Inflector;

abstract class Forge
{

    /**
     * Auto-trigger on method
     * @static
     * @param $class
     * @param $method
     * @param array $args
     * @return mixed
     */
    public static function trigger($class, $method, array $args = [])
    {
        return call_user_func_array([$class, Inflector::camelize($method, true)], $args);
    }

    /**
     * Load an action object
     * @static
     * @param $class
     * @param $method
     * @param array $args
     * @return array|null
     */
    public static function action($class, $method, array $args = [])
    {
        Event::fire('forge.action', [$class, $method]);

        // physical path
        $file = path('app') . '\\' . $class . '.php';
        if(file_exists($file))
        {
            // namespace path
            if(($action = new $class()) instanceof \wicked\core\Controller)
            {
                $method = Inflector::camelize($method, true);
                if(method_exists($action, $method))
                {
                    return []
                        + (array)$action->_before()
                        + (array)static::trigger($action, $method, $args)
                        + (array)$action->_after();
                }
            }
        }

        return null;
    }

    /**
     * Shortcut : controller
     * @static
     * @param $controller
     * @param $action
     * @param array $args
     * @return array|null
     */
    public static function controller($controller, $action, array $args = [])
    {
        $path = config('folder::controller') . '\\' . Inflector::camelize($controller) . 'Controller';
        return static::action($path, $action, $args);
    }

    /**
     * Shortcut : module
     * @static
     * @param $controller
     * @param $action
     * @param array $args
     * @return array|null
     */
    public static function module($controller, $action, array $args = [])
    {
        $path = config('folder::module') . '\\' . Inflector::camelize($controller) . 'Module';
        return static::action($path, $action, $args);
    }

    /**
     * Load view
     * @static
     * @param $path
     * @param array $data
     */
    public static function view($path, $data = [])
    {
        Event::fire('forge.view', [$path]);
        extract((array)$data);
        include path('app') . $path . '.php';
    }

    /**
     * Shortcut : template
     * @static
     * @param $path
     * @param array $data
     */
    public static function template($path, $data = [])
    {
        static::view(config('folder::template') . '/' . $path, $data);
    }

    /**
     * Shortcut : block
     * @static
     * @param $path
     * @param array $data
     */
    public static function block($path, $data = [])
    {
        static::view(config('folder::block') . '/' . $path, $data);
    }

}
